<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/12/18
 * Time: 14:45
 */
namespace app\admin\controller;
use think\Db;
class Market extends Base
{
    /*
     * 行情分类
     */
    public function cate(){
        if(request()->isAjax ()){
            extract(input());
            $map['mar_status'] = 1;
            if(isset($key)&&$key!="")
            {
                $map['mar_name'] = ['like',"%" . $key . "%"];
            }
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('mar_type')->where($map)->count();
            $lists = Db::name('mar_type')
                ->where($map)
                ->field('mar_id,mar_name,mar_create_time,mar_update_time')
                ->page($Nowpage,$limits)
                ->order('mar_create_time desc')
                ->select();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch('market/cate');
    }

    /*
     * 添加分类
     */
    public function cateAdd(){
        if(request()->isPost ()){
            extract(input());
            $time = date('Y-m-d H:i:s');
            $param = [
                'mar_name'=>$cate,
                'mar_create_time'=>$time,
                'mar_update_time'=>$time
            ];
            $res = Db::name('mar_type')->insert($param);
            if($res){
                return json(['code'=>200,'msg'=>'添加成功']);
            }else{
                return json(['code'=>100,'msg'=>'添加失败']);
            }
        }
        return $this->fetch('market/cate_add');
    }

    /*
     * 编辑分类
     */
    public function cateEdit(){
        if(request()->isPost ()){
            extract(input());
            $param = [
                'mar_name'=>$cate,
                'mar_update_time'=>date('Y-m-d H:i:s')
            ];
            $res = Db::name('mar_type')
                ->where('mar_id',$id)
                ->update($param);
            if($res){
                return json(['code'=>200,'msg'=>'编辑成功']);
            }else{
                return json(['code'=>100,'msg'=>'编辑失败']);
            }
        }
        $info = Db::name('mar_type')
            ->where('mar_id',input('id'))
            ->field('mar_id,mar_name')
            ->find();
        return $this->fetch('market/cate_edit',['info'=>$info]);
    }

    /*
     * 删除分类
     */
    public function cateDel(){
        $res = Db::name('mar_type')
            ->where('mar_id',input('id'))
            ->setField ('mar_status',2);
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }

    /*
     * 新闻资讯
     */
    public function message(){
        if(request()->isAjax ()){
            extract(input());
            $map = [];
            if(isset($type)&&$type != ""){
                $map['m.mar_type'] = $type;
            }
            if(isset($key)&&$key!="")
            {
                $map['m.mar_title'] = ['like',"%" . $key . "%"];
            }
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('market m')
                ->join('mar_type t','m.mar_type = t.mar_id','left')
                ->where($map)
                ->count();
            $lists = Db::name('market m')
                ->join('mar_type t','m.mar_type = t.mar_id','left')
                ->where($map)
                ->field(['m.mar_id','t.mar_name','m.mar_title','m.mar_num','mar_image','m.mar_content','m.mar_create_time','m.mar_update_time'])
                ->page($Nowpage,$limits)
                ->order('mar_create_time desc')
                ->select ();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        $arr = Db::name('mar_type')
            ->where('mar_status',1)
            ->field('mar_id,mar_name')
            ->select ();
        return $this->fetch('market/message',['arr'=>$arr]);
    }


    /*
     * 添加资讯
     */
    public function messageAdd(){
        if(request()->isPost ()){
            extract(input());
            $time = date('Y-m-d H:i:s');
            $param = [
                'mar_type'=>$type,
                'mar_title'=>$title,
                'mar_num'=>$num,
                'mar_image'=>$img,
                'mar_content'=>$content,
                'mar_create_time'=>$time,
                'mar_update_time'=>$time
            ];
            $res = Db::name('market')->insert($param);
            if($res){
                return json(['code'=>200,'msg'=>'添加成功']);
            }else{
                return json(['code'=>100,'msg'=>'添加失败']);
            }
        }
        $arr = Db::name('mar_type')
            ->where('mar_status',1)
            ->field('mar_id,mar_name')
            ->select ();
        return $this->fetch('market/message_add',['arr'=>$arr]);
    }

    /*
     * 资讯编辑
     */
    public function messageEdit(){
        if(request()->isPost ()){
            extract(input());
            $param = [
                'mar_type'=>$type,
                'mar_title'=>$title,
                'mar_num'=>$num,
                'mar_image'=>$img,
                'mar_content'=>$content,
                'mar_update_time'=>date('Y-m-d H:i:s')
            ];
            $res = Db::name('market')
                ->where('mar_id',input('id'))
                ->update($param);
            if($res){
                return json(['code'=>200,'msg'=>'编辑成功']);
            }else{
                return json(['code'=>100,'msg'=>'编辑失败']);
            }
        }
        $arr = Db::name('mar_type')
            ->where('mar_status',1)
            ->field('mar_id,mar_name')
            ->select ();
        $info = Db::name('market')
            ->where('mar_id',input('id'))
            ->field(['mar_id','mar_type','mar_title','mar_num','mar_image','mar_content'])
            ->find();
        return $this->fetch('market/message_edit',['arr'=>$arr,'info'=>$info]);
    }

    /*
     * 删除资讯
     */
    public function messageDel(){
        $res = Db::name('market')->delete(input('id'));
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }
}